package com.gupt.reggie.controller;/*
@author name
@data 2023/4/17-18:23
*/

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gupt.reggie.common.R;
import com.gupt.reggie.entity.Employee;
import com.gupt.reggie.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")                               //boot 用该注解接受前端的json数据
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        Employee emp = employeeService.login(request,employee);
        return R.success(emp);  //返回一个账号信息
    }


    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("成功退出");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工信息：{}",employee.toString());
        //使用md5jia加密密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        
        //设置创建or修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获得当前登录用户
        Long empId = (Long)request.getSession().getAttribute("employee");

        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);

        employeeService.save(employee);
        return R.success("员工新增成功");
    }


    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){    //name: 前端查询(搜索栏）具体员工姓名
        Page pageInfo = employeeService.page(page,pageSize,name);
        return R.success(pageInfo);
    }


    /**
     * 修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){   //传id 和 static 谁状态是什么

        log.info("修改信息id static{}：",employee.toString());

        long id = Thread.currentThread().getId();  //验证controller filter ObjectHandler 为同一个线程
        log.info("当前线程id为：{}",id);


        //由common下的MyMetaObjectHandler处理公共类

//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getEmpById(@PathVariable Long id){
        log.info("根据id查询员工");
        Employee emp = employeeService.getById(id);
        if(emp != null){
            return R.success(emp);
        }
            return R.error("查询失败");
    }

}
